package com.dyna.core.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author zhoucaiwang
 * @date 2023/8/15
 */

public class SecurityEncodeUtil {

    private final static int PSW_LENGTH_LOW = 8;

    private final static int PSW_LENGTH_UP = 20;

    /**
     * 密码加密
     * @param password 密码
     * @return 加密
     */
    public static String encoding(String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(password);
    }

    /**
     * 校验密码
     * @param rawPassword 原始密码
     * @param encodedPassword 加密密码
     * @return Boolean
     */
    public static Boolean matches(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.matches(rawPassword,encodedPassword);
    }

    public static Boolean weakPassword(String password) {
        if (StrUtil.isBlank(password) ||
                password.length() < PSW_LENGTH_LOW ||
                password.length() > PSW_LENGTH_UP ||
                !WeakPassWordUtil.isLetterDigit(password)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

}